package com.demo.webapp.validator.adapter;

import com.opensymphony.xwork2.validator.FieldValidator;

/**
 * 必填项转换
 * {validate:{required:true, messages:{required:'Please enter xxx'}}}
 * 		<pre>
 * &lt;field-validator type="requiredstring"&gt;
 *	&lt;param name="trim"&gt;true&lt;/param&gt;
 *	&lt;message key="user.oldpassword.required" /&gt;
 * &lt;/field-validator&gt;</pre>
 * @author ryuu.kk
 *
 */
public class RequireValidateAdapter implements ValidateAdapter {

	/**
	 * 后端服务器类型
	 */
	private static final String VALIDATION_SERVER = "requiredstring";
	
	/**
	 * 前置Metadata字符串
	 */
	private static final String VALIDATION_CLIENT = "required";
	
	public RequireValidateAdapter(){}
	
	@Override
	public TransWrapper convert(FieldValidator fV) {

		TransWrapper wrapper = new TransWrapper();
		wrapper.setKey(VALIDATION_CLIENT);
		wrapper.setType(VALIDATION_SERVER);
		wrapper.setMessage(fV.getMessage(fV.getFieldName()));
		wrapper.setValidation("true");
		
		return wrapper;
	}

	@Override
	public boolean support(FieldValidator fV) {
		return (fV != null && (fV.getValidatorType().equalsIgnoreCase(VALIDATION_SERVER) 
				|| fV.getValidatorType().equalsIgnoreCase("required")));
	}

}
